package stdying.handyweibo.presenter;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;


import com.sina.weibo.sdk.auth.Oauth2AccessToken;
import com.sina.weibo.sdk.auth.WeiboAuth;
import com.sina.weibo.sdk.auth.WeiboAuthListener;
import com.sina.weibo.sdk.auth.sso.SsoHandler;
import com.sina.weibo.sdk.exception.WeiboException;

import javax.inject.Inject;

import stdying.handyweibo.openapi.TokenSession;
import stdying.handyweibo.openapi.weibo.AccessTokenKeeper;
import stdying.handyweibo.openapi.weibo.Constants;
import stdying.handyweibo.ui.auth.WBAuthActivity;

/**
 * Description: WeiboAuthPresenter
 * Author: liulili
 * Created: 2015-11-21
 */
public class WeiboAuthPresenter implements IPresenter {

    private static final String TAG="WeiboAuthPresenter";
    /**
     * 封装了 "access_token"，"expires_in"，"refresh_token"，并提供了他们的管理功能
     */
    private Oauth2AccessToken mAccessToken;

    private WBAuthActivity mWbAuthActivity;

    /** 微博 Web 授权类，提供登陆等功能  */
    WeiboAuth mWeiboAuth;
    /** 注意：SsoHandler 仅当 SDK 支持 SSO 时有效 */
    SsoHandler mSsoHandler;

    @Inject
    public WeiboAuthPresenter(WBAuthActivity weWbAuthActivity) {
        this.mWbAuthActivity = weWbAuthActivity;
    }

    public void weiboAuthLogin() {
        Log.d(TAG,"-- 微博登录");
        mWeiboAuth = new WeiboAuth(mWbAuthActivity, Constants.APP_KEY, Constants.REDIRECT_URL, Constants.SCOPE);
        mSsoHandler = new SsoHandler(mWbAuthActivity, mWeiboAuth);
        mSsoHandler.authorize(new Authlistener());
    }

    /**
     * 微博sso授权回调
     */
    public void ssoCallBack(int requestCode, int resultCode, Intent data) {
        if (mSsoHandler != null) {
            mSsoHandler.authorizeCallBack(requestCode, resultCode, data);
        }
    }

    @Override
    public void destroy() {

    }

    /**
     * 微博认证授权回调类。
     * 1. SSO 授权时，需要在 {@link #onActivityResult} 中调用 {@link SsoHandler#authorizeCallBack} 后，
     * 该回调才会被执行。
     * 2. 非 SSO 授权时，当授权结束后，该回调就会被执行。
     * 当授权成功后，请保存该 access_token、expires_in、uid 等信息到 SharedPreferences 中。
     */
    class Authlistener implements WeiboAuthListener {

        @Override
        public void onComplete(Bundle bundle) {

            mAccessToken = Oauth2AccessToken.parseAccessToken(bundle);
            if (mAccessToken.isSessionValid()) {
                //保存授权类型
                TokenSession.savaTokenType(mWbAuthActivity,TokenSession.TOKEN_TYPE_WEIBO);
                AccessTokenKeeper.writeAccessToken(mWbAuthActivity, mAccessToken);
                mWbAuthActivity.authLoginSuccess();
            } else {
                // 当您注册的应用程序签名不正确时，就会收到 Code，请确保签名正确
                String code = bundle.getString("code");
                mWbAuthActivity.authLoginFailure();
            }

        }

        @Override
        public void onWeiboException(WeiboException e) {
            mWbAuthActivity.authLoginFailure();
        }

        @Override
        public void onCancel() {
            mWbAuthActivity.authLoginCancle();
        }
    }
}
